package com.ruoyi.comprehensive.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.comprehensive.domain.PreventionEnclosure;
import com.ruoyi.comprehensive.domain.VentilationPreventionExamine;
import com.ruoyi.comprehensive.dto.VentilationDto;
import com.ruoyi.comprehensive.mapper.PreventionEnclosureMapper;
import com.ruoyi.comprehensive.mapper.VentilationPreventionExamineMapper;
import com.ruoyi.comprehensive.service.IVentilationPreventionExamineService;
import com.ruoyi.comprehensive.vo.PreventionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.comprehensive.mapper.VentilationPreventionMapper;
import com.ruoyi.comprehensive.domain.VentilationPrevention;
import com.ruoyi.comprehensive.service.IVentilationPreventionService;

/**
 * 通风管理Service业务层处理
 * 
 * @author hcc
 * @date 2023-12-13
 */
@Service
public class VentilationPreventionServiceImpl implements IVentilationPreventionService 
{
    @Autowired
    private VentilationPreventionMapper ventilationPreventionMapper;
    @Autowired
    private IVentilationPreventionExamineService ventilationPreventionExamineService;
    /**
     * 查询通风管理
     * 
     * @param id 通风管理主键
     * @return 通风管理
     */
    @Override
    public VentilationPrevention selectVentilationPreventionById(String id)
    {
        return ventilationPreventionMapper.selectVentilationPreventionById(id);
    }

    /**
     * 查询通风管理列表
     * 
     * @param ventilationPrevention 通风管理
     * @return 通风管理
     */
    @Override
    public List<VentilationPrevention> selectVentilationPreventionList(VentilationPrevention ventilationPrevention)
    {
        return ventilationPreventionMapper.selectVentilationPreventionList(ventilationPrevention);
    }

    /**
     * 新增通风管理
     * 
     * @param ventilationPrevention 通风管理
     * @return 结果
     */
    @Override
    public int insertVentilationPrevention(VentilationPrevention ventilationPrevention)
    {
        VentilationPrevention ventilationPrevention1 = new VentilationPrevention();
        BeanUtils.copyProperties(ventilationPrevention,ventilationPrevention1);
        ventilationPrevention1.setExamineStatus(0L);
        ventilationPrevention1.setCreateByName(SecurityUtils.getUsername());
        ventilationPrevention1.setCreateBy(SecurityUtils.getUserId().toString());
        ventilationPrevention1.setCreateAt(new Date());
        ventilationPrevention1.setUpdateAt(new Date());
        ventilationPrevention1.setUpdateByName(SecurityUtils.getUsername());
        ventilationPrevention1.setUpdateBy(SecurityUtils.getUserId().toString());
        return ventilationPreventionMapper.insertVentilationPrevention(ventilationPrevention1);
    }

    /**
     * 修改通风管理
     * 
     * @param ventilationPrevention 通风管理
     * @return 结果
     */
    @Override
    public int updateVentilationPrevention(VentilationPrevention ventilationPrevention) {
        if (ventilationPrevention.getExamineStatus() != null && ventilationPrevention.getExamineStatus()==1L){
            VentilationPrevention ventilationPrevention1 = ventilationPreventionMapper.selectVentilationPreventionById(ventilationPrevention.getId());
            if (ventilationPrevention1 != null){
                VentilationPreventionExamine ventilationPreventionExamine = new VentilationPreventionExamine();
                BeanUtils.copyProperties(ventilationPrevention1,ventilationPreventionExamine);
                ventilationPreventionExamine.setVentilationPreventionId(ventilationPrevention1.getId());
                ventilationPreventionExamine.setExamineStatus(1L);
                ventilationPreventionExamineService.insertVentilationPreventionExamine(ventilationPreventionExamine);
            }
        }
        return ventilationPreventionMapper.updateVentilationPrevention(ventilationPrevention);
    }

    /**
     * 批量删除通风管理
     * 
     * @param ids 需要删除的通风管理主键
     * @return 结果
     */
    @Override
    public int deleteVentilationPreventionByIds(String[] ids)
    {
        return ventilationPreventionMapper.deleteVentilationPreventionByIds(ids);
    }

    /**
     * 删除通风管理信息
     * 
     * @param id 通风管理主键
     * @return 结果
     */
    @Override
    public int deleteVentilationPreventionById(String id)
    {
        return ventilationPreventionMapper.deleteVentilationPreventionById(id);
    }

    /**
     * 查询通风管理列表
     * @param ventilationPrevention
     * @return
     */
    @Override
    public List<VentilationDto> getVentilationPreventionList(VentilationPrevention ventilationPrevention) {
        List<VentilationDto> ventilationPreventionList = ventilationPreventionMapper.getVentilationPreventionList(ventilationPrevention);
        return ventilationPreventionList;
    }

}
